/*
 * Created by JFormDesigner on Fri Nov 22 16:18:52 CST 2024
 */

package com.hafu.view.kehu;

import java.awt.event.*;
import com.hafu.model.KhInfo;
import com.hafu.service.KhInfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author qw
 */
public class ManageKeHuFrm extends JFrame {
   public KhInfoService khInfoService=new KhInfoService();
    private int chooseld;
    public ManageKeHuFrm() {
        
        initComponents();
        fillTable();
    }

    private void fillTable() {
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        ArrayList<KhInfo> khInfoList = khInfoService.getKhInfoByName(new KhInfo());
        for (KhInfo khInfo : khInfoList) {
          Object[] rowData = {khInfo.getId(),khInfo.getName(),khInfo.getTel()};
          dtm.addRow(rowData);
        }
    }

    private void button1(ActionEvent e) {
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        // TODO add your code here//查询事件
        String name=textField1.getText();
        KhInfo khInfo = new KhInfo();
        khInfo.setName(name);
        ArrayList<KhInfo> khInfoByName = khInfoService.getKhInfoByName(khInfo);
        if (khInfoByName.size()>0){
            for (KhInfo khInfo1 : khInfoByName){
                Object[] rowData = {khInfo1.getId(),khInfo1.getName(),khInfo1.getTel()};
                dtm.addRow(rowData);
            }
        }else {
            JOptionPane.showMessageDialog(null,"没有找到客户信息");
        }
    }

    private void table1MouseClicked(MouseEvent e) {
        int row=table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        chooseld=(int)dtm.getValueAt(row,0);
        String name=(String)dtm.getValueAt(row, 1);
        String tel=table1.getValueAt(row,2).toString();
        textField2.setText(name);
        textField3.setText(tel);
    }

    private void button2(ActionEvent e) {
        String name = textField2.getText();
        String tel = textField3.getText();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int row=table1.getSelectedRow();
        int id=(int)dtm.getValueAt(row,0);
        if (StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"客户姓名不能为空");
        }
        if (StringUtil.isEmpty(tel)){
            JOptionPane.showMessageDialog(null,"客户电话不能为空");
        }
        KhInfo khInfo = new KhInfo(name, tel);
        Boolean updateKhInfo = khInfoService.updateKhInfo(khInfo);
        if (updateKhInfo){
            JOptionPane.showMessageDialog(null,"修改成功");
            fillTable();
        }else {
            JOptionPane.showMessageDialog(null,"修改失败");
        }
    }
    private void button3(ActionEvent e) {
        int row=table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int id=(int)dtm.getValueAt(row,0);
        Boolean delKhInfo = khInfoService.delKhInfo(id);
        if (delKhInfo){
            JOptionPane.showMessageDialog(null,"删除成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"删除失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        label3 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();
        label4 = new JLabel();

        //======== this ========
        setTitle("managekehubioadan");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5ba2\u6237\u540d\u5b57");

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {"", null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                }
            ));
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u5ba2\u6237\u540d\u5b57");

        //---- label3 ----
        label3.setText("text");

        //---- button2 ----
        button2.setText("\u4fee\u6539");

        //---- button3 ----
        button3.setText("\u5220\u9664");

        //---- label4 ----
        label4.setText("\u5ba2\u6237\u7535\u8bdd");

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(41, 41, 41)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(6, 6, 6)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label2)
                                .addComponent(label3)
                                .addComponent(label4))
                            .addGap(24, 24, 24)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                                .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                            .addGap(126, 126, 126)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(button2)
                                .addComponent(button3)))
                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 489, GroupLayout.PREFERRED_SIZE)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label1)
                            .addGap(67, 67, 67)
                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)
                            .addGap(81, 81, 81)
                            .addComponent(button1)))
                    .addContainerGap(193, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button1)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
                    .addGap(38, 38, 38)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 209, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2))
                    .addGap(26, 26, 26)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label4)
                        .addComponent(button3))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(label3, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(126, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField2;
    private JTextField textField3;
    private JButton button2;
    private JButton button3;
    private JLabel label4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
